/**
 * 用户信息
 * */
export default {
  namespaced: true,
  state: {
    // 用户信息
    info: {},
    // 用户名
    username: '',
    user: '',
    hrDepartmentName: '',
    hrAreaName: '',
    loginUserInfo: null
  },
  actions: {
    /**
     * @description 设置用户数据
     * @param {Object} state vuex state
     * @param {Object} dispatch vuex dispatch
     * @param {*} info info
     */
    set ({ state, dispatch }, info) {
      return new Promise(async resolve => {
        // store 赋值
        state.info = info;
        // 持久化
        await dispatch('admin/db/set', {
          dbName: 'sys',
          path: 'user.info',
          value: info,
          user: true
        }, { root: true });
        // end
        resolve();
      })
    },
    /**
     * @description 从数据库取用户数据
     * @param {Object} state vuex state
     * @param {Object} dispatch vuex dispatch
     */
    load ({ state, dispatch }) {
      return new Promise(async resolve => {
        // store 赋值
        state.info = await dispatch('admin/db/get', {
          dbName: 'sys',
          path: 'user.info',
          defaultValue: {},
          user: true
        }, { root: true });
        // end
        resolve();
      })
    },
  },
  mutations: {
    SET_USERNAME: (state, data) => {
      state.username = data
    },
    SET_USER: (state, data) => {
      state.user = data
    },
    SET_HRDEPARTMENTNAME: (state, data) => {
      state.hrDepartmentName = data
    },
    SET_HRAREANAME: (state, data) => {
      state.hrAreaName = data
    },
    SET_USERINFO: (state, data) => {
      state.loginUserInfo = data
    },
  },
  getters: {
    /**
     * @description 获取用户名
     * @param {Object} state vuex state
     * @param {Object} dispatch vuex dispatch
     */
    getUsername: (state, getters) => (param) => {
      return state.username
    },
    getUser: (state, getters) => (param) => {
      return state.user
    },
    getHrDepartmentName: (state, getters) => (param) => {
      return state.hrDepartmentName
    },
    getHrAreaName: (state, getters) => (param) => {
      return state.hrAreaName
    },
    getLoginUserInfo: (state, getters) => (param) => {
      return state.loginUserInfo
    },
  }
}
